import request from '../utils/request';

/**
 *  创建工厂接口
 * @param {string} areaUuid 区域uuid
 * @param {string} name 工厂名称
 * @param {string} contactName 联系人姓名
 * @param {number} contactMobile 联系人电话
 * @param {number} areaCode 区域代码
 * @param {number} latitude 纬度
 * @param {number} longitude 经度
 * @param {string} location 地址
 */
export function createFactoryApi(areaUuid, name, contactName, contactMobile, areaCode, latitude, longitude, location) {
  return request({
    url: '/factories',
    method: 'post',
    data: {
      area_uuid: areaUuid || null,
      name: name,
      contact_name: contactName,
      contact_mobile: contactMobile,
      area_code: areaCode,
      latitude: latitude,
      longitude: longitude,
      location: location
    }
  });
}

/**
 * 删  除工厂接口
 * @ param {string} uuid uuid
 */
export function deleteFactoryApi(uuid) {
  return request({
    url: '/factories/' + uuid,
    method: 'delete'
  });
}

/**
 * 更  新工厂接口
 *  @param {string} uuid uuid
 * @param {string} areaUuid 区域uuid
 * @param {string} name 工厂名称
 * @param {string} contactName 联系人姓名
 * @param {string} contactMobile 联系人电话
 * @param {number} areaCode 区域代号
 * @param {number} latitude 纬度
 * @param {number} longitude 经度
 * @param {string} location 工厂位置
 */
export function updateFactoryApi(uuid, areaUuid, name, contactName, contactMobile, areaCode, latitude, longitude, location) {
  return request({
    url: '/factories/' + uuid,
    method: 'put',
    data: Object.assign(
      {
        name: name,
        contact_name: contactName,
        contact_mobile: contactMobile,
        area_code: areaCode,
        latitude: latitude,
        longitude: longitude,
        location: location
      },
      areaUuid ? { area_uuid: areaUuid } : null
    )
  });
}

/**
 * 获取工厂列表接口
 * @param {number} pageIndex 页码下标，默认从0开始
 *  @param {number} pageSize 每页大小
 *  @param {string} sortType 正序or倒序
 * @param {string} orderBy 排序字段
 *   @param {string} areaUuid 区域Uuid
 *  @param {string} name 工厂名称模糊查询
 */
export function getFactoryListApi(pageIndex, pageSize, sortType, orderBy, areaUuid, name) {
  return request({
    url: '/factories',
    method: 'get',
    params: Object.assign({
      page_index: pageIndex,
      page_size: pageSize,
      sort_type: sortType,
      order_by: orderBy
    },
    areaUuid ? { area_uuid: areaUuid } : null,
    name ? { name: name } : null
    )
  });
}

/**
 * 获取工 厂详情接口
 * @pa ram {string} uuid uuid
 */
export function getFactoryDetailApi(uuid) {
  return request({
    url: '/factories/' + uuid,
    method: 'get'
  });
}

/**
 * 查询工厂接口
 * @param {string} name 类型名称
 * @param {string} take 获取数量 5-100
 */
export function queryFactoryListApi(name, take) {
  return request({
    url: '/factories/query',
    method: 'get',
    params: { name, take }
  });
}
